#include "SDL.h"
#include "time.h"

bool is_hello;
bool is_main;
bool is_help;
bool is_lose;
bool is_new_ga;
bool is_new_vit;
bool is_new_ngan;
bool running;
int velx_ga;
int velx_ga_new;
int velx_vit;
int velx_vit_new;
int velx_ngan;
int velx_ngan_new;
int vely_trung;
int vely_trung_new;
int random;
int egg_break;
int eat_shit;
int eat_egg ;
bool is_egg_ga;
bool is_egg_ga_new;
bool is_egg_vit;
bool is_egg_vit_new;
bool is_egg_ngan;
bool is_egg_ngan_new;
int time_trung_ga;
int time_trung_ga_new;
int time_trung_vit;
int time_trung_vit_new;
int time_trung_ngan;
int time_trung_ngan_new;

SDL_Rect offset_egg_break;

SDL_Rect offset_shit_1;

SDL_Rect offset_lose;

SDL_Rect offset_try_again;

SDL_Rect offset_help_1;

SDL_Rect offset_back;

SDL_Rect offset_start;

SDL_Rect offset_help;

SDL_Rect offset_ro;

SDL_Rect offset_ga;
	
SDL_Rect offset_vit;

SDL_Rect offset_wall;

SDL_Rect offset_wall_1;

SDL_Rect offset_number_egg;

SDL_Rect offset_number_shit;

SDL_Rect offset_ngan;

SDL_Rect offset_ga_new;

SDL_Rect offset_vit_new;

SDL_Rect offset_ngan_new;

//trung
SDL_Rect offset_trung_ga;
	
SDL_Rect offset_trung_ga_new;

SDL_Rect offset_trung_vit;

SDL_Rect offset_trung_vit_new;

SDL_Rect offset_trung_ngan;

SDL_Rect offset_trung_ngan_new;

SDL_Rect crop_trung_ga;

SDL_Rect crop_trung_ga_new;

SDL_Rect crop_trung_vit;

SDL_Rect crop_trung_vit_new;

SDL_Rect crop_trung_ngan;

SDL_Rect crop_trung_ngan_new;

//shit
SDL_Rect crop_shit_ga;

SDL_Rect crop_shit_ga_new;

SDL_Rect crop_shit_vit;

SDL_Rect crop_shit_vit_new;

SDL_Rect crop_shit_ngan;

SDL_Rect crop_shit_ngan_new;


void Move_Animal(SDL_Surface* screen,SDL_Surface* image, SDL_Rect &offset, int &velx)
{
	offset.x += velx;
	if(offset.x > 500 || offset.x < 0)
		velx = -velx;

	SDL_BlitSurface(image,NULL,screen,&offset);

}

void Move_Basket(SDL_Surface* screen,SDL_Surface* image, SDL_Rect &offset, int velx, SDL_Event occur, Uint8* keystates)
{
	bool is_mouse_click = false;
	int x, y;
	SDL_GetMouseState(&x,&y);
	if(keystates[SDLK_LEFT])
		offset.x -= 4;
	else if(keystates[SDLK_RIGHT])
	{
		if(offset.x < 500)
			offset.x += 4;
	}

	if(occur.type == SDL_MOUSEMOTION)
	{
		if(x <= 500)
			offset.x = x;
	}
	
	SDL_BlitSurface(image,NULL,screen,&offset);
}

void Move_Egg_Shit(SDL_Surface* screen,SDL_Surface* image_egg, SDL_Surface* image_shit, SDL_Rect &offset_ga, SDL_Rect &offset_egg, SDL_Rect &crop_egg, SDL_Rect &crop_shit, int &vely, int &time, int &random, bool &is_egg)
{
	if(time >= 0)
	{
		offset_egg.y += vely;
		crop_egg.w = 48;
	}
	if(offset_egg.y > 650)
	{
		crop_egg.h = 0;
		crop_shit.w = 0;
	}	
	if (time % 900 == 0)
	{
		random = rand() % 10;
		if(random <= 6)
		{
			is_egg = true;
			crop_egg.h = 48;
		
			offset_egg.y = 0;
			offset_egg.x = offset_ga.x;
		}
		else
		{
			is_egg = false;
			crop_shit.w = 48;
			offset_egg.y = 0;
			offset_egg.x = offset_ga.x;
		}	
	}
	SDL_BlitSurface(image_egg,&crop_egg,screen,&offset_egg);
	SDL_BlitSurface(image_shit,&crop_shit,screen,&offset_egg);
	time++;
}

void Xu_Ly_Hung_Trung(bool is_egg, SDL_Rect &offset_ro, SDL_Rect &offset_trung, int &eat_egg, int &egg_break, int &eat_shit, int &vely_trung)
{
	if(is_egg == true)
	{
		if((offset_trung.x >= offset_ro.x) && ((offset_trung.x + offset_trung.w) <= (offset_ro.x + offset_ro.w)) && (offset_trung.y == offset_ro.y))
		{
			eat_egg++;
		}
		else if(((offset_trung.x < offset_ro.x) && ((offset_trung.x + offset_trung.w) < (offset_ro.x + offset_ro.w)) && (offset_trung.y == offset_ro.y)) || ((offset_trung.x > offset_ro.x) && ((offset_trung.x + offset_trung.w) > (offset_ro.x + offset_ro.w)) && (offset_trung.y == offset_ro.y)))
		{
			egg_break++;
		}
	}
	else
	{
		if((offset_trung.x > offset_ro.x) && ((offset_trung.x + offset_trung.w) < (offset_ro.x + offset_ro.w)) && (offset_trung.y == offset_ro.y))
		{
			eat_shit++;
		}
	}
	if (eat_egg >= 20)
	{
		eat_egg = 0;
		vely_trung++;
	}
}

void Xu_Ly_Them(SDL_Surface* screen,SDL_Surface* image, SDL_Rect &offset, int &velx,SDL_Surface* image_egg, SDL_Surface* image_shit, SDL_Rect &offset_ga, SDL_Rect &offset_egg, SDL_Rect &crop_egg, SDL_Rect &crop_shit, int &vely, int &time, int &random, bool &is_egg, SDL_Rect &offset_ro, SDL_Rect &offset_trung, int &eat_egg, int &egg_break, int &eat_shit, int &vely_trung)
{
	Move_Animal(screen,image,offset_ga,velx);
	Move_Egg_Shit(screen,image_egg,image_shit,offset_ga,offset_egg,crop_egg,crop_shit,vely_trung,time,random,is_egg);
	Xu_Ly_Hung_Trung(is_egg,offset_ro,offset_egg,eat_egg,egg_break,eat_shit,vely_trung);
}

SDL_Rect KhoiTao(int x, int y, int w, int h)
{
	SDL_Rect kq;
	kq.x = x;
	kq.y = y;
	kq.w = w;
	kq.h = h;
	return kq; 
}

void Xu_Ly_Button(SDL_Surface* screen, SDL_Surface* image, SDL_Surface* image1, SDL_Rect offset)
{
	int x, y;
	SDL_GetMouseState(&x,&y);

	if(((x >= offset.x) && (x <= (offset.x + 200)) && ((y >= offset.y) && (y <= (offset.y + 70)))))
	{
		SDL_BlitSurface(image1, NULL,screen,&offset);
	}
	else
	{
		SDL_BlitSurface(image, NULL,screen,&offset);
	}
}

void KhoiTao_First()
{
	is_new_ga = false;
	is_new_vit = false;
	is_new_ngan = false;

	velx_ga = 1;
	velx_ga_new = 1;
	velx_vit = 1;
	velx_vit_new = 1;
	velx_ngan = 1;
	velx_ngan_new = 1;
	vely_trung = 1;
	vely_trung_new = 1;
	egg_break = 0;
	eat_shit = 0;
	eat_egg = 0;
	is_egg_ga = false;
	is_egg_ga_new = false;
	is_egg_vit = false;
	is_egg_vit_new = false;
	is_egg_ngan = false;
	is_egg_ngan_new = false;
	time_trung_ga = 0;
	time_trung_ga_new = 0;
	time_trung_vit = -350;
	time_trung_vit_new = -350;
	time_trung_ngan = -550;
	time_trung_ngan_new = -550;

	offset_shit_1 = KhoiTao(630,380,NULL,NULL);

	offset_egg_break = KhoiTao(630,0,NULL,NULL);

	offset_lose = KhoiTao(50,150,NULL,NULL);

	offset_try_again = KhoiTao(200,250,NULL,NULL);

	offset_help_1 = KhoiTao(200,150,NULL,NULL);

	offset_back = KhoiTao(300,500,NULL,NULL);

	offset_start = KhoiTao(300,200,NULL,NULL);

	offset_help = KhoiTao(300,350,NULL,NULL);

	offset_ro = KhoiTao(0,600,100,NULL);

	offset_ga = KhoiTao(0,0,NULL,NULL);
	
	offset_vit = KhoiTao(200,0,NULL,NULL);

	offset_ngan = KhoiTao(400,0,NULL,NULL);

	offset_wall = KhoiTao(600,0,NULL,NULL);

	offset_wall_1 = KhoiTao(600,340,NULL,NULL);

	offset_number_egg = KhoiTao(630,150,NULL,NULL);

	offset_number_shit = KhoiTao(630,550,NULL,NULL);

	offset_ga_new = KhoiTao(0,0,NULL,NULL);

	offset_vit_new = KhoiTao(200,0,NULL,NULL);

	offset_ngan_new = KhoiTao(400,0,NULL,NULL);

	//trung
	offset_trung_ga = KhoiTao(offset_ga.x,0,40,48);

	offset_trung_ga_new = KhoiTao(offset_ga.x,0,40,48);

	offset_trung_vit = KhoiTao(offset_vit.x,0,40,48);

	offset_trung_vit_new = KhoiTao(offset_vit.x,0,40,48);

	offset_trung_ngan = KhoiTao(offset_ngan.x,0,40,48);

	offset_trung_ngan_new = KhoiTao(offset_ngan.x,0,40,48);

	crop_trung_ga = KhoiTao(0,0,40,48);

	crop_trung_ga_new = KhoiTao(0,0,40,48);

	crop_trung_vit = KhoiTao(0,0,0,48);

	crop_trung_vit_new = KhoiTao(0,0,0,48);

	crop_trung_ngan = KhoiTao(0,0,0,48);

	crop_trung_ngan_new = KhoiTao(0,0,0,48);

	//shit
	crop_shit_ga = KhoiTao(0,0,0,49);

	crop_shit_ga_new = KhoiTao(0,0,0,49);

	crop_shit_vit = KhoiTao(0,0,0,49);

	crop_shit_vit_new = KhoiTao(0,0,0,49);

	crop_shit_ngan = KhoiTao(0,0,0,49);

	crop_shit_ngan_new = KhoiTao(0,0,0,49);
}

int main(int argc, char* args[])
{
	is_hello = true;
	is_main = false;
	is_help = false;
	is_lose = false;

	running = true;
	srand(time(NULL));

	SDL_Event occur;
	Uint8* keystates = SDL_GetKeyState(NULL);

	SDL_Surface *ro;
	ro = SDL_LoadBMP("Images/ro.bmp");

	SDL_Surface *ga;
	ga = SDL_LoadBMP("Images/ga.bmp");

	SDL_Surface *vit;
	vit = SDL_LoadBMP("Images/vit.bmp");

	SDL_Surface *ngan;
	ngan = SDL_LoadBMP("Images/ngan.bmp");

	SDL_Surface *trung;
	trung = SDL_LoadBMP("Images/egg.bmp");

	SDL_Surface *shit;
	shit = SDL_LoadBMP("Images/shit.bmp");

	SDL_Surface *start;
	start = SDL_LoadBMP("Images/start.bmp");

	SDL_Surface *start1;
	start1 = SDL_LoadBMP("Images/start1.bmp");

	SDL_Surface *help;
	help = SDL_LoadBMP("Images/help.bmp");

	SDL_Surface *help_1;
	help_1 = SDL_LoadBMP("Images/Help-1.bmp");

	SDL_Surface *help1;
	help1 = SDL_LoadBMP("Images/Help1.bmp");

	SDL_Surface *back;
	back = SDL_LoadBMP("Images/back.bmp");

	SDL_Surface *back1;
	back1 = SDL_LoadBMP("Images/back1.bmp");

	SDL_Surface *lose;
	lose = SDL_LoadBMP("Images/lose.bmp");

	SDL_Surface *try_again;
	try_again = SDL_LoadBMP("Images/tryagain.bmp");

	SDL_Surface *try_again1;
	try_again1 = SDL_LoadBMP("Images/tryagain1.bmp");

	SDL_Surface *wall;
	wall = SDL_LoadBMP("Images/wall.bmp");

	SDL_Surface *wall_1;
	wall_1 = SDL_LoadBMP("Images/wall-1.bmp");

	SDL_Surface *number_0;
	number_0 = SDL_LoadBMP("Images/0.bmp");

	SDL_Surface *number_1;
	number_1 = SDL_LoadBMP("Images/1.bmp");

	SDL_Surface *number_2;
	number_2 = SDL_LoadBMP("Images/2.bmp");

	SDL_Surface *number_3;
	number_3 = SDL_LoadBMP("Images/3.bmp");

	SDL_Surface *image_egg_break;
	image_egg_break = SDL_LoadBMP("Images/egg-break.bmp");

	SDL_Surface *image_shit_1;
	image_shit_1 = SDL_LoadBMP("Images/shit-1.bmp");

	//init SDL
	if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
	{
		running = false;
	}
	
	SDL_Surface *screen;

	screen = SDL_SetVideoMode(800,700,32,SDL_HWSURFACE);

	int x, y;
	SDL_GetMouseState(&x,&y);
	
	SDL_WM_SetCaption("GLTest",NULL);

	if(screen == NULL)
	{
		running = false;
	}
	//Khoi tao

	KhoiTao_First();

	//main application loop

	while(running == true)
	{
		SDL_PollEvent(&occur);

		if(occur.type == SDL_QUIT)
		{
			running = false;
		}

		//drawing occurs here
		if(is_hello == true)
		{
			int x, y;
			SDL_GetMouseState(&x,&y);

			SDL_FillRect(screen,NULL,0);
			if((x >= offset_start.x) && (x <= (offset_start.x + 200)) && (y >= offset_start.y) && (y <= (offset_start.y + 70)))
			{
				if(occur.button.button == SDL_BUTTON_LEFT)
				{
					is_hello = false;
					is_main = true;
				}
			}

			if((x >= offset_help.x) && (x <= (offset_help.x + 200)) && (y >= offset_help.y) && (y <= (offset_help.y + 70)))
			{
				if(occur.button.button == SDL_BUTTON_LEFT)
				{
					is_hello = false;
					is_help = true;
				}
			}

			Xu_Ly_Button(screen,start,start1,offset_start);
			
			Xu_Ly_Button(screen,help,help1,offset_help);
			
			SDL_Flip(screen);
		}

		if(is_help == true)
		{
			int x, y;
			SDL_GetMouseState(&x,&y);

			SDL_FillRect(screen,NULL,0);
			SDL_BlitSurface(help_1, NULL,screen,&offset_help_1);
			
			Xu_Ly_Button(screen,back,back1,offset_back);

			if((x >= offset_back.x) && (x <= (offset_back.x + 200)) && (y >= offset_back.y) && (y <= (offset_back.y + 50)))
			{
				if(occur.button.button == SDL_BUTTON_LEFT)
				{
					is_hello = true;
					is_help = false;
				}
			}
			SDL_Flip(screen);
		}

		if(is_lose == true)
		{
			int x, y;
			SDL_GetMouseState(&x,&y);

			SDL_FillRect(screen,NULL,0);
			SDL_BlitSurface(lose, NULL,screen,&offset_lose);


			Xu_Ly_Button(screen,try_again,try_again1,offset_try_again);

			if((x >= offset_try_again.x) && (x <= (offset_try_again.x + 300)) && (y >= offset_try_again.y) && (y <= (offset_try_again.y + 100)))
			{
				if(occur.button.button == SDL_BUTTON_LEFT)
				{
					KhoiTao_First();
					is_main = true;
					is_lose = false;
					egg_break = 0;
					eat_shit = 0;
				}
			}

			SDL_Flip(screen);
		}

		if(is_main == true)
		{
			SDL_FillRect(screen,NULL,0);

			//draw our image
			if(keystates[SDLK_g])
			{
				is_new_ga = true;
			}
			if(is_new_ga == true)
			{
				Xu_Ly_Them(screen,ga,offset_ga_new,velx_ga_new,trung,shit,offset_ga_new,offset_trung_ga_new,crop_trung_ga_new,crop_shit_ga_new,vely_trung,time_trung_ga_new,random,is_egg_ga_new,offset_ro,offset_trung_ga_new,eat_egg,egg_break,eat_shit,vely_trung);
			}

			if(keystates[SDLK_v])

			{
				is_new_vit = true;
			}
			if(is_new_vit == true)
			{
				Xu_Ly_Them(screen,vit,offset_vit_new,velx_vit_new,trung,shit,offset_vit_new,offset_trung_vit_new,crop_trung_vit_new,crop_shit_vit_new,vely_trung,time_trung_vit_new,random,is_egg_vit_new,offset_ro,offset_trung_vit_new,eat_egg,egg_break,eat_shit,vely_trung);
			}

			if(keystates[SDLK_n])

			{
				is_new_ngan = true;
			}
			if(is_new_ngan == true)
			{
				Xu_Ly_Them(screen,ngan,offset_ngan_new,velx_ngan_new,trung,shit,offset_ngan_new,offset_trung_ngan_new,crop_trung_ngan_new,crop_shit_ngan_new,vely_trung,time_trung_ngan_new,random,is_egg_ngan_new,offset_ro,offset_trung_ngan_new,eat_egg,egg_break,eat_shit,vely_trung);
			}

			Move_Animal(screen,ga,offset_ga,velx_ga);
			Move_Animal(screen,vit,offset_vit,velx_vit);
			Move_Animal(screen,ngan,offset_ngan,velx_ngan);
			Move_Basket(screen,ro,offset_ro,4,occur,keystates);		
		
			Move_Egg_Shit(screen,trung,shit,offset_ga,offset_trung_ga,crop_trung_ga,crop_shit_ga,vely_trung,time_trung_ga,random,is_egg_ga);
			Move_Egg_Shit(screen,trung,shit,offset_vit,offset_trung_vit,crop_trung_vit,crop_shit_vit,vely_trung,time_trung_vit,random,is_egg_vit);
			Move_Egg_Shit(screen,trung,shit,offset_ngan,offset_trung_ngan,crop_trung_ngan,crop_shit_ngan,vely_trung,time_trung_ngan,random,is_egg_ngan);
		
			Xu_Ly_Hung_Trung(is_egg_ga,offset_ro,offset_trung_ga,eat_egg,egg_break,eat_shit,vely_trung);
			Xu_Ly_Hung_Trung(is_egg_vit,offset_ro,offset_trung_vit,eat_egg,egg_break,eat_shit,vely_trung);
			Xu_Ly_Hung_Trung(is_egg_ngan,offset_ro,offset_trung_ngan,eat_egg,egg_break,eat_shit,vely_trung);

			SDL_BlitSurface(wall, NULL,screen,&offset_wall);

			SDL_BlitSurface(wall_1, NULL,screen,&offset_wall_1);

			SDL_BlitSurface(image_egg_break, NULL,screen,&offset_egg_break);

			SDL_BlitSurface(number_0, NULL,screen,&offset_number_egg);

			SDL_BlitSurface(image_shit_1, NULL,screen,&offset_shit_1);

			SDL_BlitSurface(number_0, NULL,screen,&offset_number_shit);

			if(egg_break >= 3 || eat_shit >= 3)
			{
				is_lose = true;
				is_main = false;
			}

			switch (egg_break)
			{
				case 0:
					SDL_BlitSurface(number_0, NULL,screen,&offset_number_egg);
					break;
				case 1:
					SDL_BlitSurface(number_1, NULL,screen,&offset_number_egg);
					break;
				case 2:
					SDL_BlitSurface(number_2, NULL,screen,&offset_number_egg);
					break;
				case 3:
					SDL_BlitSurface(number_3, NULL,screen,&offset_number_egg);
					break;
			}
			switch (eat_shit)
			{
				case 0:
					SDL_BlitSurface(number_0, NULL,screen,&offset_number_shit);
					break;
				case 1:
					SDL_BlitSurface(number_1, NULL,screen,&offset_number_shit);
					break;
				case 2:
					SDL_BlitSurface(number_2, NULL,screen,&offset_number_shit);
					break;
				case 3:
					SDL_BlitSurface(number_3, NULL,screen,&offset_number_shit);
					break;
			}

			SDL_Flip(screen);
		}
	}

	SDL_Quit();
	return 0;

}